package com.along.auth.mapper;

import com.along.auth.entity.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/*
 *@Author: liujinyi
 *@CreateTime: 2025-08-05
 *@Description:
 *@Version: 1.0
 */
@Mapper
public interface SysUserMapper {
    @Results(id = "rbacDeptMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "deptId", column = "dept_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "email", column = "email"),
            @Result(property = "phonenumber", column = "phonenumber"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "avatar", column = "avatar"),
            @Result(property = "password", column = "password"),
            @Result(property = "status", column = "status"),
            @Result(property = "delFlag", column = "del_flag"),
            @Result(property = "createBy", column = "create_by"),
            @Result(property = "loginIp", column = "login_ip"),
            @Result(property = "loginDate", column = "login_date"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateBy", column = "update_by"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "remark", column = "remark")
    })
    // 根据用户名查询用户
    @Select("SELECT * FROM sys_user WHERE user_name = #{username} AND del_flag = '0'")
    SysUser selectByUsername(@Param("username") String username);

    // 根据用户ID查询用户
    @Select("SELECT * FROM sys_user WHERE user_id = #{userId} AND del_flag = '0'")
    SysUser selectByUserId(@Param("userId") Long userId);

    // 查询用户列表
    @Select("<script>" +
            "SELECT * FROM sys_user WHERE del_flag = '0'" +
            "<if test='userName != null and userName != \"\"'> AND user_name LIKE CONCAT('%', #{userName}, '%')</if>" +
            "<if test='phonenumber != null and phonenumber != \"\"'> AND phonenumber LIKE CONCAT('%', #{phonenumber}, '%')</if>" +
            "<if test='status != null and status != \"\"'> AND status = #{status}</if>" +
            " ORDER BY user_id DESC" +
            "</script>")
    List<SysUser> selectUserList(SysUser user);

    // 新增用户
    @Insert("INSERT INTO sys_user(" +
            "dept_id, user_name, nick_name, user_type, email, phonenumber, sex, " +
            "avatar, password, status, del_flag, create_by, create_time, remark" +
            ") VALUES(" +
            "#{deptId}, #{userName}, #{nickName}, #{userType}, #{email}, #{phonenumber}, #{sex}, " +
            "#{avatar}, #{password}, #{status}, #{delFlag}, #{createBy}, #{createTime}, #{remark}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insertUser(SysUser user);

    // 更新用户信息
    @Update("<script>" +
            "UPDATE sys_user" +
            "<set>" +
            "<if test='deptId != null'>dept_id = #{deptId},</if>" +
            "<if test='userName != null and userName != \"\"'>user_name = #{userName},</if>" +
            "<if test='nickName != null and nickName != \"\"'>nick_name = #{nickName},</if>" +
            "<if test='email != null and email != \"\"'>email = #{email},</if>" +
            "<if test='phonenumber != null and phonenumber != \"\"'>phonenumber = #{phonenumber},</if>" +
            "<if test='sex != null and sex != \"\"'>sex = #{sex},</if>" +
            "<if test='avatar != null and avatar != \"\"'>avatar = #{avatar},</if>" +
            "<if test='password != null and password != \"\"'>password = #{password},</if>" +
            "<if test='status != null and status != \"\"'>status = #{status},</if>" +
            "<if test='loginIp != null and loginIp != \"\"'>login_ip = #{loginIp},</if>" +
            "<if test='loginDate != null'>login_date = #{loginDate},</if>" +
            "<if test='updateBy != null and updateBy != \"\"'>update_by = #{updateBy},</if>" +
            "<if test='remark != null'>remark = #{remark},</if>" +
            "update_time = #{updateTime}" +
            "</set>" +
            " WHERE user_id = #{userId}" +
            "</script>")
    int updateUser(SysUser user);

    // 删除用户（逻辑删除）
    @Update("UPDATE sys_user SET del_flag = '2' WHERE user_id = #{userId}")
    int deleteUserById(@Param("userId") Long userId);

    // 重置用户密码
    @Update("UPDATE sys_user SET password = #{password} WHERE user_id = #{userId}")
    int resetUserPassword(@Param("userId") Long userId, @Param("password") String password);

    // 更新登录信息
    @Update("UPDATE sys_user SET login_ip = #{loginIp}, login_date = #{loginDate} WHERE user_id = #{userId}")
    int updateLoginInfo(@Param("userId") Long userId, @Param("loginIp") String loginIp, @Param("loginDate") Date loginDate);

    // 检查用户名是否唯一
    @Select("SELECT COUNT(1) FROM sys_user WHERE user_name = #{userName} AND del_flag = '0'")
    int checkUsernameUnique(@Param("userName") String userName);

    // 检查手机号是否唯一
    @Select("SELECT COUNT(1) FROM sys_user WHERE phonenumber = #{phonenumber} AND del_flag = '0'")
    int checkPhoneUnique(@Param("phonenumber") String phonenumber);

    // 检查邮箱是否唯一
    @Select("SELECT COUNT(1) FROM sys_user WHERE email = #{email} AND del_flag = '0'")
    int checkEmailUnique(@Param("email") String email);
}
